class Site::SearchController < ApplicationController
  def index
    @search_type = params[:search_type]
    q = params[:q]
    if q.blank?
      search_all(@search_type)
    else
      search_result(@search_type)
    end
    render :layout => search_layout(@search_type)
  end
  
  private 
  def search_layout(search_type)
    case search_type
      when RETYPE
        "site/requirement"
      when DETYPE
        "site/design"
      when INTYPE
        "site/info"
    end
  end
  
  def search_result(t)
    case t
        when RETYPE
          @requirements = Requirement.search(q,:page => params[:page], :per_page => PERPAGE)
          @title = "搜索创意需求"
        when DETYPE
          @designs = Work.search(q,:page => params[:page], :per_page => PERPAGE)
          @title = "搜索创意设计"
        when INTYPE
          @infos = Info.search(q,:page => params[:page], :per_page => PERPAGE)
          @title = "搜索创意资讯"
      end
  end
  
  def search_all(t)
    case t
        when RETYPE
          @requirements = Requirement.paginate(:all,:page => params[:page], :per_page => PERPAGE, :order => "updated_at DESC")
          @title = "搜索创意需求"
        when DETYPE
          @designs = Work.paginate(:all,:page => params[:page], :per_page => PERPAGE, :order => "updated_at DESC")
          @title = "搜索创意设计"
        when INTYPE
          @infos = Info.paginate(:all,:page => params[:page], :per_page => PERPAGE, :order => "updated_at DESC")
          @title = "搜索创意资讯"
      end
  end

end
